package com.jdrx.sw.sewage.api.web;

import com.jdrx.platform.commons.rest.beans.vo.ResponseVO;
import com.jdrx.platform.commons.rest.exception.BizException;
import com.jdrx.platform.commons.rest.factory.ResponseFactory2;
import com.jdrx.sw.sewage.beans.anno.MQDeviceCacheRefresh;
import com.jdrx.sw.sewage.beans.anno.UseDeviceCacheRefresh;
import com.jdrx.sw.sewage.beans.dto.DevStorageDTO;
import com.jdrx.sw.sewage.beans.dto.DevStorageTypeQueryDTO;
import com.jdrx.sw.sewage.beans.dto.DeviceNumSetDTO;
import com.jdrx.sw.sewage.beans.dto.UnlimitedDTO;
import com.jdrx.sw.sewage.beans.entity.DeviceNumSetPO;
import com.jdrx.sw.sewage.beans.vo.*;
import com.jdrx.sw.sewage.beans.vo.base.PageVO;
import com.jdrx.sw.sewage.common.constant.Constant;
import com.jdrx.sw.sewage.common.enums.StorageDevType;
import com.jdrx.sw.sewage.common.helper.HttpUtils;
import com.jdrx.sw.sewage.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

import static com.jdrx.sw.sewage.common.constant.Constant.TRANSPARENT_USERNAME_FEILD;

@RestController
@RequestMapping(value = "/api/devicestorage")
@Api(value = "设备入库模块")
public class DeviceStorageApi {

    @Autowired
    private MaterielServiceImpl materielServiceImpl;
    @Autowired
    private FactoryServiceImpl factoryServiceImpl;
    @Autowired
    private CategoryServiceImpl categoryServiceImpl;
    @Autowired
    private DevStorageTypeServiceImpl devStorageTypeServiceImpl;
    @Autowired
    private DevStorageRecordServiceImpl devStorageRecordServiceImpl;
    @Autowired
    private DevStorageServiceImpl devStorageServiceImpl;
    @Autowired
    private MessageSubscribeServiceImpl messageSubscribeServiceImpl;
    @Autowired
    private DeviceNumSetServiceImpl deviceNumSetServiceImpl;

    @ApiOperation(value = "获取设备编号配置")
    @RequestMapping(value = "getDeviceNumSet", method = RequestMethod.GET)
    public ResponseVO<DeviceNumSetPO> getDeviceNumSet(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                                      @RequestHeader(Constant.TENANT_ID) String tenantId) {
        return ResponseFactory2.ok(deviceNumSetServiceImpl.getDeviceNumSet());
    }

    @ApiOperation(value = "设备编号配置初始化")
    @RequestMapping(value = "getDeviceNumSetInitial", method = RequestMethod.POST)
    public ResponseVO<DeviceNumSetPO> getDeviceNumSetInitial(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                                             @RequestHeader(Constant.TENANT_ID) String tenantId,
                                                             @RequestBody DeviceNumSetDTO deviceNumSetDTO) {
        deviceNumSetServiceImpl.getDeviceNumSetInitial(deviceNumSetDTO);
        return ResponseFactory2.ok();
    }

    @ApiOperation(value = "查询物料类型和设备品类")
    @RequestMapping(value = "selectMaterielAndDevStorage", method = RequestMethod.GET)
    public ResponseVO<List<MaterielVO>> selectMaterielAndDevStorage(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                                                    @RequestHeader(Constant.TENANT_ID) String tenantId) {
        return ResponseFactory2.ok(materielServiceImpl.selectMaterielAndDevStorage());
    }

    @ApiOperation(value = "查询物料和品类和生产厂家")
    @RequestMapping(value = "findFactoryAndDevStorageAndMateriel", method = RequestMethod.POST)
    public ResponseVO<Map<String, Object>> findFactoryAndDevStorageAndMateriel(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                                                               @RequestHeader(Constant.TENANT_ID) String tenantId) throws BizException {
        return ResponseFactory2.ok(devStorageTypeServiceImpl.findFactoryAndDevStorageAndMateriel());
    }

    @ApiOperation(value = "设备入库")
    @RequestMapping(value = "deviceStorage", method = RequestMethod.POST)
    public ResponseVO deviceStorage(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                    @RequestHeader(Constant.TENANT_ID) String tenantId,
                                    @RequestBody @Valid DevStorageDTO devStorageDTO) throws BizException {
        return devStorageTypeServiceImpl.deviceStorage(devStorageDTO, dataNode);
    }

    @ApiOperation(value = "设备入库-导入模板下载")
    @PostMapping("deviceStorageDownload")
    public ResponseVO deviceStorageDownload(HttpServletResponse response) throws BizException {
        devStorageTypeServiceImpl.deviceStorageDownload(response);
        return ResponseFactory2.ok();
    }

    @ApiOperation(value = "设备入库-导入校验")
    @RequestMapping(value = "deviceStorageImportCheck", method = RequestMethod.POST)
    public ResponseVO deviceStorageImportCheck(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                               @RequestHeader(Constant.TENANT_ID) String tenantId,
                                               MultipartFile file) throws IOException {
        return devStorageTypeServiceImpl.deviceStorageImportCheck(file);
    }

    @ApiOperation(value = "设备入库-导入")
    @RequestMapping(value = "devStorageImport", method = RequestMethod.POST)
    public ResponseVO devStorageImport(@RequestHeader(TRANSPARENT_USERNAME_FEILD) String currUserName,
                                       @RequestHeader(Constant.DATA_NODE) String dataNode,
                                       @RequestHeader(Constant.TENANT_ID) String tenantId,
                                       MultipartFile file) throws IOException {
        devStorageTypeServiceImpl.deviceStorageImport(file);
        return ResponseFactory2.ok("导入成功！");
    }

    @ApiOperation(value = "分页查询")
    @RequestMapping(value = "selectByPage", method = RequestMethod.POST)
    public ResponseVO<PageVO<DevStorageTypeVO>> selectByPage(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                                             @RequestHeader(Constant.TENANT_ID) String tenantId,
                                                             @RequestBody DevStorageTypeQueryDTO devStorageTypeQueryDTO) {
        return ResponseFactory2.ok(devStorageTypeServiceImpl.selectByPage(devStorageTypeQueryDTO));
    }

    @ApiOperation(value = "变更记录")
    @RequestMapping(value = "selectStorageRecord", method = RequestMethod.GET)
    public ResponseVO<List<DeviceStorageRecordVO>> selectStorageRecord(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                                                       @RequestHeader(Constant.TENANT_ID) String tenantId,
                                                                       @RequestParam("storageTypeId") Long storageTypeId) {
        return ResponseFactory2.ok(devStorageRecordServiceImpl.selectStorageRecord(storageTypeId));
    }

    @ApiOperation(value = "入库设备列表")
    @RequestMapping(value = "selectDevStorageList", method = RequestMethod.GET)
    public ResponseVO<List<DevStorageVO>> selectDevStorageList(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                                               @RequestHeader(Constant.TENANT_ID) String tenantId,
                                                               @RequestParam StorageDevType storageDevType,
                                                               @RequestParam Long storageTypeId) {
        return ResponseFactory2.ok(devStorageServiceImpl.selectDevStorageList(storageDevType, storageTypeId));
    }

    @ApiOperation(value = "入库设备数")
    @RequestMapping(value = "selectDevNum", method = RequestMethod.GET)
    public ResponseVO<List<Map<Integer, Integer>>> selectDevNum(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                                                @RequestHeader(Constant.TENANT_ID) String tenantId,
                                                                @RequestParam Long storageTypeId) {
        return ResponseFactory2.ok(devStorageServiceImpl.selectDevNum(storageTypeId));
    }

    @ApiOperation(value = "入库设备详情")
    @RequestMapping(value = "selectDevInfo", method = RequestMethod.GET)
    public ResponseVO<DevStorageInfoVO> selectDevInfo(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                                      @RequestHeader(Constant.TENANT_ID) String tenantId,
                                                      @RequestParam StorageDevType storageDevType,
                                                      @RequestParam Long devId) {
        return ResponseFactory2.ok(devStorageServiceImpl.selectDevInfo(storageDevType, devId));
    }

    @ApiOperation(value = "入库设备删除")
    @RequestMapping(value = "deleteDev", method = RequestMethod.DELETE)
    public ResponseVO<DevStorageInfoVO> deleteDev(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                                  @RequestHeader(Constant.TENANT_ID) String tenantId,
                                                  @RequestParam Long storageTypeId,
                                                  @RequestParam Long devId) {
        devStorageServiceImpl.deleteDev(storageTypeId, devId);
        return ResponseFactory2.ok();
    }

    @ApiOperation(value = "使用设备报废")
    @RequestMapping(value = "scrap", method = RequestMethod.DELETE)
    @UseDeviceCacheRefresh
    @MQDeviceCacheRefresh
    public ResponseVO<DevStorageInfoVO> scrap(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                              @RequestHeader(Constant.TENANT_ID) String tenantId,
                                              @RequestParam("devId") Long devId) {
        devStorageServiceImpl.scrap(devId);
        return ResponseFactory2.ok();
    }

    @ApiOperation(value = "入库设备修改")
    @RequestMapping(value = "updateDev", method = RequestMethod.POST)
    public ResponseVO updateDev(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                @RequestHeader(Constant.TENANT_ID) String tenantId,
                                @RequestBody DevStorageDTO devStorageDTO) {
        devStorageServiceImpl.updateDev(devStorageDTO);
        return ResponseFactory2.ok();
    }

    @ApiOperation(value = "获取小程序二维码")
    @PostMapping(value = "getUnlimited")
    public void getUnlimited(HttpServletResponse response,
                             @RequestHeader(Constant.DATA_NODE) String dataNode,
                             @RequestHeader(Constant.TENANT_ID) String tenantId,
                             @RequestBody UnlimitedDTO unlimitedDTO) throws Exception {
        InputStream inputStream = messageSubscribeServiceImpl.getUnlimited(unlimitedDTO);
        HttpUtils.writeFile(response, inputStream);
    }
}
